package com.syaccp.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.syaccp.mapper.BankRemitMapper;
import com.syaccp.model.*;
import com.syaccp.request.BankRemitAddRequest;
import com.syaccp.request.CustomerDetailSelectRequest;
import com.syaccp.response.CustomerSelectResponse;
import com.syaccp.service.*;
import com.syaccp.utils.Result;
import com.syaccp.utils.SecurityUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

@Service
public class BankRemitServiceImpl  extends ServiceImpl<BankRemitMapper, BankRemit> implements BankRemitService {
    @Resource
    private DictDataService dictDataService;
    @Resource
    private ExchargePackageService eps;
    @Resource
    private CustomerService customerService;

    @Resource
    private StoreCouponService scs;




    @Override
    public Result findBankRemit(CustomerDetailSelectRequest customerDetailSelectRequest) {

        PageHelper.startPage(customerDetailSelectRequest.getPage(), customerDetailSelectRequest.getLimit());
        QueryWrapper qw=new QueryWrapper();
        qw.eq("customer_id", customerDetailSelectRequest.getId());
        PageInfo pageInfo = new PageInfo<>(this.baseMapper.selectList(qw));
        return Result.success(pageInfo);
    }

    @Override
    @Transactional
    public Result topUp(BankRemitAddRequest bankRemitAddRequest) {
        SysUser loginUser = SecurityUtil.getLoginUser();
        //获取充值方式
        SysDictData sysDictData = dictDataService.getBaseMapper().selectById(bankRemitAddRequest.getBankId());
        //获取充值包
        ExchargePackage packageByMoney = eps.findPackageByMoney(bankRemitAddRequest.getRemitMoney());
        //获取充值的客户信息
        CustomerSelectResponse data = (CustomerSelectResponse)customerService.findCustomerById(bankRemitAddRequest.getCustomerId()).getData();
        Customer customer = new Customer();
        BeanUtils.copyProperties(data,customer);
        customer.setBalance(customer.getBalance()+bankRemitAddRequest.getRemitMoney());
        //修改用户余额
        int i = customerService.getBaseMapper().updateById(customer);
        if (i==0){
            throw new RuntimeException();
        }
        BankRemit br=new BankRemit();
        //余额
        br.setAccountBalance(customer.getBalance());
        //充值金额
        br.setRemitMoney(bankRemitAddRequest.getRemitMoney());
        //充值方式
        br.setBankId(sysDictData.getId());
        br.setBankName(sysDictData.getLabel());
        //客服
        br.setBussesId(loginUser.getId());
        br.setBussesName(loginUser.getName());
        //客户信息
        br.setCustomerId(customer.getId());
        br.setCustomerName(customer.getCustomerName());
        //新增充值记录
        int insert = this.baseMapper.insert(br);
        if (insert==0){
            throw new RuntimeException();
        }
        if (packageByMoney!=null){
            StoreCoupon storeCoupon=new StoreCoupon();
            //优惠券名称
            storeCoupon.setName(customer.getCustomerName()+"_"+packageByMoney.getExchargeName());
            //优惠券金额
            storeCoupon.setMoney(packageByMoney.getCouponMoney());
            //优惠券数量
            storeCoupon.setNum(packageByMoney.getCouponNum());
            //客户id
            storeCoupon.setCustomerId(customer.getId());
            //客户名称
            storeCoupon.setCustomerName(customer.getCustomerName());
            //充值包id
            storeCoupon.setRechargeId(packageByMoney.getId());
            //充值包名称
            storeCoupon.setRechargeName(packageByMoney.getExchargeName());
            //创建者
            storeCoupon.setCreateBy(customer.getId());
            //新增优惠券
            int insert1 = scs.addStoreCoupon(storeCoupon);
            if (insert1==0){
                throw new RuntimeException();
            }
            /*
            //如果优惠券新增是每一张一条记录，使用循环
            for (int j = 0; j < packageByMoney.getCouponNum(); j++) {
                StoreCoupon storeCoupon=new StoreCoupon();
                //优惠券名称
                storeCoupon.setName(packageByMoney.getExchargeName()+(j+1));
                //优惠券金额
                storeCoupon.setMoney(packageByMoney.getCouponMoney());
                 //客户id
                storeCoupon.setCustomerId(customer.getId());
                //客户名称
                storeCoupon.setCustomerName(customer.getCustomerName());
                //充值包id
                storeCoupon.setRechargeId(packageByMoney.getId());
                //充值包名称
                storeCoupon.setRechargeName(packageByMoney.getExchargeName());
                //创建者
                storeCoupon.setCreateBy(customer.getId());
                int insert1 = scs.getBaseMapper().insert(storeCoupon);
                if (insert1==0){
                    throw new RuntimeException();
                }

            }*/
        }


        return Result.success(insert);
    }
}
